package com.starlz.jpa.entity;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Proxy;
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;

import javax.persistence.*;
import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @Author: wangzi
 * @Date: 2018/11/20 09:15
 */
@Entity
@Table(name = "user", schema = "test")
@Proxy(lazy = false)
public class LocalUser {
    private String id;
    private String username;
    private String password;

    @Id
    @Column(name = "id")
    @GenericGenerator(name="idGenerator", strategy="uuid") //这个是hibernate的注解/生成32位UUID
    @GeneratedValue(generator="idGenerator")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Basic
    @Column(name = "username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Basic
    @Column(name = "password")
    public String getPassword() {
        return password;
    }


    public List<LocalRole> roles;

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LocalUser localUser = (LocalUser) o;
        return Objects.equals(id, localUser.id) &&
                Objects.equals(username, localUser.username) &&
                Objects.equals(password, localUser.password);
    }

    @Override
    public int hashCode() {

        return Objects.hash(id, username, password);
    }

    @ManyToMany
    @JoinTable(
            name = "user_role",
            joinColumns = {@JoinColumn(name = "user_id",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")}
    )
    public List<LocalRole> getRoles() {
        return roles;
    }

    public void setRoles(List<LocalRole> roles) {
        this.roles = roles;
    }
}
